/**
******************************************************************************
* @file           : studio_geo_c.h
* @author         : wangyingjie
* @brief          : 几何矢量类型 C 语言版
* @attention      : None
* @date           : 2025/5/10
******************************************************************************
*/

#include "studio_geo_c.h"

////////////// 点 //////////////

studio_point_c studio_point_init(double xx, double yy)
{
    studio_point_c point = {xx, yy};
    return point;
}

bool studio_point_equal(const studio_point_c* p1, const studio_point_c* p2)
{
    bool is_equal = fabs(p1->x - p2->x) <= AO_EPSILON && fabs(p1->y - p2->y) <= AO_EPSILON;
    return is_equal;
}

////////////// 线 //////////////

studio_line_c studio_line_c_init(void)
{
    studio_line_c line;
    line.size = 0;
    line.capacity = 4;
    line.data = (studio_point_c*)malloc(line.capacity * sizeof(studio_point_c));
    return line; // 注意: 返回结构体副本 (需配合 C99 或更高标准)
}

void studio_line_c_destroy(studio_line_c* line)
{
    if (line->data)
    {
        free(line->data);
        line->data = NULL;
        line->size = 0;
        line->capacity = 0;
    }
}

void studio_line_c_add_point(studio_line_c* line, studio_point_c point)
{
    // 容量不足时扩容 (策略: 增加固定容量 10)
    if (line->size >= line->capacity)
    {
        line->capacity += 10;
        studio_point_c* new_data = (studio_point_c*)realloc(line->data, line->capacity * sizeof(studio_point_c));
        if (!new_data)
        {
            // 此处可添加错误处理 (例如: 终止程序或返回错误码)
            return;
        }
        line->data = new_data;
    }

    // 添加新元素
    line->data[line->size++] = point;
}

bool studio_line_c_remove_point(studio_line_c* line, unsigned int index)
{
    if (index >= line->size)
    {
        return false; // 索引无效
    }
    // 将后续点前移
    for (unsigned int i = index; i < line->size - 1; i++)
    {
        line->data[i] = line->data[i + 1];
    }
    line->size--;
    // 缩小容量以节省内存
    if (line->capacity > 10 && line->size < line->capacity / 2)
    {
        unsigned int new_capacity = line->capacity / 2;
        studio_point_c* new_data = (studio_point_c*)realloc(line->data, new_capacity * sizeof(studio_point_c));
        if (new_data)
        {
            line->data = new_data;
            line->capacity = new_capacity;
        }
    }
    return true;
}

unsigned int studio_line_c_size(const studio_line_c* line)
{
    return line->size;
}


studio_point_c studio_line_c_get_point(const studio_line_c* line, unsigned int index)
{
    studio_point_c tmp;
    if (index < line->size)
    {
        tmp = line->data[index];
        return tmp;
    }
    return tmp; // 越界返回0，0点
}

bool studio_line_c_set_point(studio_line_c* line, unsigned int index, studio_point_c point)
{
    bool status = false;
    if (index > line->size)
    {
        return status;
    }
    line->data[index] = point;
    status = true;
    return status;
}


////////////// 矩形 //////////////

studio_rect_c studio_rect_init(double l, double t, double r, double b)
{
    studio_rect_c rect = {studio_point_init(l, t), studio_point_init(r, b)};
    return rect;
}

void studio_rect_correct(studio_rect_c* rect)
{
    if (rect->left_top.x > rect->right_bottom.x)
    {
        double temp = rect->left_top.x;
        rect->left_top.x = rect->right_bottom.x;
        rect->right_bottom.x = temp;
    }
    if (rect->left_top.y < rect->right_bottom.y)
    {
        double temp = rect->left_top.y;
        rect->left_top.y = rect->right_bottom.y;
        rect->right_bottom.y = temp;
    }
}

bool studio_rect_intersect(const studio_rect_c* r1, const studio_rect_c* r2)
{
    return !(r1->right_bottom.x < r2->left_top.x || r1->left_top.x > r2->right_bottom.x || r1->right_bottom.y > r2->
                                                                                                                left_top.y || r1->left_top.y < r2->right_bottom.y);
}

////////////// 圆 //////////////

studio_circle_c studio_circle_init(studio_point_c center, double radius)
{
    studio_circle_c circle = {center, radius};
    return circle;
}

double studio_circle_area(const studio_circle_c* circle)
{
    return AO_M_PI * circle->radius * circle->radius;
}

////////////// 三角形 //////////////

studio_triangle_c studio_triangle_init(studio_point_c a, studio_point_c b, studio_point_c c)
{
    studio_triangle_c triangle = {a, b, c};
    return triangle;
}

double studio_triangle_oriented_area(const studio_triangle_c* triangle)
{
    return (triangle->a.x * (triangle->b.y - triangle->c.y) + triangle->b.x * (triangle->c.y - triangle->a.y) + triangle
                                                                                                                ->c.x * (triangle->a.y - triangle->b.y)) / 2.0;
}

double studio_triangle_area(const studio_triangle_c* triangle)
{
    return fabs(studio_triangle_oriented_area(triangle));
}

////////////// 椭圆 //////////////

studio_ellipse_c studio_ellipse_init(studio_point_c center, double rx, double ry)
{
    studio_ellipse_c ellipse = {center, rx, ry};
    return ellipse;
}

double studio_ellipse_area(const studio_ellipse_c* ellipse)
{
    return AO_M_PI * ellipse->rx * ellipse->ry;
}

double studio_ellipse_circumference(const studio_ellipse_c* ellipse, int Ramanujan)
{
    if (Ramanujan == 1)
    {
        return AO_M_PI * (3 * (ellipse->rx + ellipse->ry) - sqrt(
                              (3 * ellipse->rx + ellipse->ry) * (ellipse->rx + 3 * ellipse->ry)));
    }
    return 2 * AO_M_PI * sqrt((ellipse->rx * ellipse->rx + ellipse->ry * ellipse->ry) / 2);
}


